﻿#region Using directives

using System;
using System.Drawing;
using System.Windows.Forms;

#endregion

namespace OpenComposite.Tools.ScrollControl
{
	[ToolboxBitmap(typeof(UserControl))]
	public partial class ScrollControl : UserControl
	{
		#region Members

		#region Private Vars

		private Control m_control = null;

		#endregion Private Vars

		#region Private Methods

		private void adaptScrollbars()
		{
			if ( m_control == null )
				return;

			m_control = panel.Controls[0];
			int ch = m_control.Height, cw = m_control.Width;
			int ph = panel.Height, pw = panel.Width;

			if ( ch > ph )
				vScrollBar.Maximum = ch - ph;
			else
				vScrollBar.Maximum = vScrollBar.Minimum;

			if ( vScrollBar.Value > vScrollBar.Maximum )
				vScrollBar.Value = vScrollBar.Maximum;

			if ( cw > pw )
				hScrollBar.Maximum = cw - pw;
			else
				hScrollBar.Maximum = hScrollBar.Minimum;

			if ( hScrollBar.Value > hScrollBar.Maximum )
				hScrollBar.Value = hScrollBar.Maximum;
		}

		#endregion Private Methods

		#region Eventhandlers

		private void vScrollBar_Scroll(object sender, ScrollEventArgs e)
		{
			if ( m_control == null )
				return;

			if ( m_control.Height <= panel.Height )
				vScrollBar.Value = 0;

			m_control.Top = -1 * vScrollBar.Value;
		}

		private void hScrollBar_Scroll(object sender, ScrollEventArgs e)
		{
			if ( m_control == null )
				return;

			if ( m_control.Width <= panel.Width )
				hScrollBar.Value = 0;

			m_control.Left = -1 * hScrollBar.Value;
		}

		private void panel_ControlAdded(object sender, ControlEventArgs e)
		{
			m_control = panel.Controls[0];

			m_control.Resize += new EventHandler(m_control_Resize);
			m_control.MouseWheel += new MouseEventHandler(m_control_MouseWheel);

			adaptScrollbars();
		}

		private void panel_ControlRemoved(object sender, ControlEventArgs e)
		{
			if ( m_control != null ) {
				m_control.Resize -= new EventHandler(m_control_Resize);
				m_control.MouseWheel -= new MouseEventHandler(m_control_MouseWheel);
				m_control = null;
			}
		}

		private void panel_Resize(object sender, EventArgs e)
		{
			adaptScrollbars();
		}

		void m_control_Resize(object sender, EventArgs e)
		{
			adaptScrollbars();
		}

		void m_control_MouseWheel(object sender, MouseEventArgs e)
		{
			int delta = vScrollBar.SmallChange * e.Delta * SystemInformation.MouseWheelScrollLines / 120;
			if ( ( Control.ModifierKeys & Keys.Shift ) == Keys.Shift ) {
				int x = hScrollBar.Value - delta;

				if ( x < hScrollBar.Minimum )
					x = hScrollBar.Minimum;
				else if ( x > hScrollBar.Maximum )
					x = hScrollBar.Maximum;

				hScrollBar.Value = x;
				m_control.Left = -1 * hScrollBar.Value;
			} else {
				int y = vScrollBar.Value - delta;

				if ( y < vScrollBar.Minimum )
					y = vScrollBar.Minimum;
				else if ( y > vScrollBar.Maximum )
					y = vScrollBar.Maximum;

				vScrollBar.Value = y;
				m_control.Top = -1 * vScrollBar.Value;
			}
		}

		#endregion Eventhandlers

		#endregion Members

		#region Interface

		#region Properties

		/// <summary>
		/// Get and set the backcolor property.
		/// </summary>
		/// <value></value>
		public Color BackGroundColor
		{
			get { return this.panel.BackColor; }
			set
			{
				if ( this.panel.BackColor != value )
					this.panel.BackColor = value;
			}
		}

		/// <summary>
		/// Get and set the BackgroundImage property.
		/// </summary>
		/// <value></value>
		public Image BackgGroundImage
		{
			get { return this.panel.BackgroundImage; }
			set
			{
				if ( this.panel.BackgroundImage != value )
					this.panel.BackgroundImage = value;
			}
		}

		#endregion Properties

		#region Methods

		public void AddControl(Control con)
		{
			if ( panel.Controls.Count > 0 )
				return;

			panel.Controls.Add(con);
		}

		public void ClearControls()
		{
			panel.Controls.Clear();
			hScrollBar.Value = 0;
			vScrollBar.Value = 0;
		}

		#endregion Methods

		#endregion Interface

		#region Constructors

		public ScrollControl()
		{
			InitializeComponent();

			this.panel.Resize += new EventHandler(this.panel_Resize);
			this.panel.ControlAdded += new System.Windows.Forms.ControlEventHandler(this.panel_ControlAdded);
			this.panel.ControlRemoved += new System.Windows.Forms.ControlEventHandler(this.panel_ControlRemoved);
		}

		#endregion Constructors
	}
}
